package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;

import com.buyouchina.bean.product.OrderForm;
import com.buyouchina.bean.product.Record;
import com.buyouchina.bean.product.Statistical;
import com.buyouchina.bean.product.Statistical.StatisticalType;
import com.buyouchina.bean.product.Statistical.TongjiAreaType;
import com.buyouchina.service.area.ProvinceService;
import com.buyouchina.service.product.OrderService;
import com.buyouchina.service.product.StatisticalService;
import com.buyouchina.web.action.BaseAction;

@Controller
public class Tongji extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {

		Tongji tongji1 = new Tongji();
		tongji1.tongjiCityLevel();
	//	tongji1.tongjiCountyLevel();
	//	tongji1.tongjiStateLevel();
	}

	public void tongjiCityLevel() {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		OrderService orderService = (OrderService) factory
				.getBean("orderServiceBean");
		StatisticalService tongjiService = (StatisticalService) factory
				.getBean("statisticalServiceBean");
		ProvinceService provinceService = (ProvinceService) factory
				.getBean("provinceServiceBean");

		Statistical st = null;
		String cityid;
		Integer productid;

		try {
			Date[] dateTime = this.todayTimeArea();
			// 查询今日的交易成功的所有订单
			List<OrderForm> orders = orderService.findByDate(dateTime[0],
					dateTime[1]);
			// 遍历每一订单保存或者更新统计记录
			for (OrderForm order : orders) {
				// 获取市级别的id
				cityid = provinceService.getCityId(order.getOutlets()
						.getProvince().getId());
				// 遍历当前订单的所有货物进出记录
				List<Record> records = new ArrayList<Record>(order.getRecords());
				for (Record record : records) {
					productid = record.getProduct().getId();
					// 验证是否存在同产品同市级别的记录
					st = tongjiService.checkCityRecord(productid, cityid,
							dateTime[0], dateTime[1]);
					if (st != null) {
						Boolean ssBoolean = st.getOrders().contains(order);
						if (!ssBoolean) {
							st.setQuantity(st.getQuantity()
									+ record.getQuantity());
							st.getOrders().add(order);
							tongjiService.update(st);
						}
					} else {
						st = new Statistical();
						st.setAreaType(TongjiAreaType.CITYLEVEL);
						st.setType(StatisticalType.PURCHASE);
						st.setDate(order.getDate());
						st.setProduct(record.getProduct());
						st.setQuantity(record.getQuantity());
						st.setProvince(provinceService.find(Integer
								.parseInt(cityid)));
						st.getOrders().add(order);
						tongjiService.save(st);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Date[] todayTimeArea() throws ParseException {
		Date[] dates = new Date[2];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startTime = sdf.format(new Date()) + " 00:00:00";
		String endTime = sdf.format(new Date()) + " 23:59:59";
		dates[0] = sdf1.parse(startTime);
		dates[1] = sdf1.parse(endTime);
		return dates;
	}

	public void tongjiCountyLevel()  {

		Statistical st = null;

		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		StatisticalService tongjiService = (StatisticalService) factory
				.getBean("statisticalServiceBean");
		ProvinceService provinceService = (ProvinceService) factory
				.getBean("provinceServiceBean");

		try {
			Date[] dateTime = this.todayTimeArea();
			List<Statistical> tongjiList = tongjiService.list(
					TongjiAreaType.CITYLEVEL, new Date(), null);
			for (Statistical tongji : tongjiList) {

				String provinceid = provinceService.getProvinceId(tongji
						.getProvince().getId());

				st = tongjiService.checkCityRecord(tongji.getProduct().getId(),
						provinceid, dateTime[0], dateTime[1]);
				if (st != null) {
					st.setQuantity(st.getQuantity() + tongji.getQuantity());
					tongjiService.update(st);
				} else {
					st = new Statistical();
					st.setAreaType(TongjiAreaType.COUNTYLEVEL);
					st.setType(StatisticalType.PURCHASE);
					st.setDate(tongji.getDate());
					st.setProduct(tongji.getProduct());
					st.setQuantity(tongji.getQuantity());
					st.setProvince(provinceService.find(Integer
							.parseInt(provinceid)));
					tongjiService.save(st);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		

	}
	
	public void tongjiStateLevel()  {

		Statistical st = null;

		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		StatisticalService tongjiService = (StatisticalService) factory
				.getBean("statisticalServiceBean");

		try {
			Date[] dateTime = this.todayTimeArea();
			List<Statistical> tongjiList = tongjiService.list(
					TongjiAreaType.COUNTYLEVEL, new Date(), null);
			for (Statistical tongji : tongjiList) {
				st = tongjiService.checkCityRecord(tongji.getProduct().getId(),
						null, dateTime[0], dateTime[1]);
				if (st != null) {
					st.setQuantity(st.getQuantity() + tongji.getQuantity());
					tongjiService.update(st);
				} else {
					st = new Statistical();
					st.setAreaType(TongjiAreaType.STATELEVEL);
					st.setType(StatisticalType.PURCHASE);
					st.setDate(tongji.getDate());
					st.setProduct(tongji.getProduct());
					st.setQuantity(tongji.getQuantity());
					tongjiService.save(st);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
